package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.WkIssueApply;
import com.ysd.lis.entity.WkIssueAssign;
import com.ysd.lis.mapper.WkIssueApplyMapper;
import com.ysd.lis.mapper.WkIssueAssignMapper;
import com.ysd.lis.request.IssueApplyParam;
import com.ysd.lis.service.WkIssueApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-03-12
 */
@Service
@Slf4j
public class WkIssueApplyServiceImpl extends ServiceImpl<WkIssueApplyMapper, WkIssueApply> implements WkIssueApplyService {
    @Autowired
    WkIssueApplyMapper wkIssueApplyMapper;
    @Autowired
    WkIssueAssignMapper wkIssueAssignMapper;
    @Override
    public Result addIssueApply(WkIssueApply wkIssueApply) {
        int insert = wkIssueApplyMapper.insert(wkIssueApply);
        if (insert > 0) {
            return Result.succ(1, "添加成功", wkIssueApply);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editIssueApply(WkIssueApply wkIssueApply) {
        int updateById = wkIssueApplyMapper.updateById(wkIssueApply);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", wkIssueApply);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result getIssueApplyById(String id) {
        WkIssueApply wkIssueApply = wkIssueApplyMapper.selectById(id);
        return Result.succ(1, "查询成功！", wkIssueApply);
    }

    @Override
    public Result findIssueApplyPageList(IssueApplyParam pageParam) {
        Page<WkIssueApply> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        MPJLambdaWrapper<WkIssueApply> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(WkIssueApply.class)
                .selectAs("sub",SysUser::getRealname,WkIssueApply::getSubmitterName)
                .leftJoin(SysUser.class,"sub",SysUser::getId,WkIssueApply::getSubmitter)
                .eq(WkIssueApply::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            mpjLambdaWrapper.and(wrapper -> wrapper.like(WkIssueApply::getTitle, pageParam.getSeachValue())
                    .or().like(WkIssueApply::getTitle, pageParam.getSeachValue()));
        }
        if(StringUtils.isNotBlank(pageParam.getAssignId())){
            mpjLambdaWrapper.eq(WkIssueApply::getAssignId,pageParam.getAssignId());
        }
        String statuss = pageParam.getStatuss();
        if(StringUtils.isNotBlank(statuss)){
            String[] split = statuss.split(",");
            mpjLambdaWrapper.in(WkIssueApply::getStatus,split);
        }
        mpjLambdaWrapper.orderByDesc(WkIssueApply::getCreateTime);
        Page<WkIssueApply> knowledgePage = wkIssueApplyMapper.selectJoinPage(page,WkIssueApply.class, mpjLambdaWrapper);
        return Result.succ(1, "查询成功", knowledgePage);
    }

    @Override
    public Result deleteIssueApply(WkIssueApply wkIssueApply) {
        int deleteById = wkIssueApplyMapper.deleteById(wkIssueApply.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result revokeIssueApply(WkIssueApply wkIssueApply) {
        int i = wkIssueApplyMapper.updateById(wkIssueApply);
        if(i>0){
            LambdaUpdateWrapper<WkIssueAssign> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(WkIssueAssign::getApplyId, wkIssueApply.getId());
            wkIssueAssignMapper.delete(lambdaUpdateWrapper);
            return Result.succ(1, "撤回成功", "");
        }else {
            return Result.fail(400, "撤回失败", "");
        }
    }

    @Override
    public Result reBackIssueApply(WkIssueApply wkIssueApply) {
        int i = wkIssueApplyMapper.updateById(wkIssueApply);
        if(i>0){
            return Result.succ(1, "退回成功", "");
        }else {
            return Result.fail(400, "退回失败", "");
        }
    }
}
